package com.jsmy.marketing.fg;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jsmy.marketing.R;
import com.jsmy.marketing.activity.CustomerActivity;
import com.jsmy.marketing.adapter.StudentFragmentAdapter;
import com.jsmy.marketing.api.RetrofitManager;
import com.jsmy.marketing.bean.BaseBean;
import com.jsmy.marketing.bean.StudentBean;
import com.jsmy.marketing.common.Link;
import com.jsmy.marketing.utils.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * Created with Android Studio.
 * User: wei_cd.
 * Date: 2018/4/17.
 * Time: 17:48.
 * To change for file | settings | file and code templates.
 */

public class StudentFragment extends BaseFragment  {


    @BindView(R.id.my_recycler)
    RecyclerView myRecycler;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    StudentFragmentAdapter adapter;
    List<StudentBean.DataBean.ListBean> list;
    int pageindex = 1;


    @Override
    protected int resLayout() {
        return R.layout.fg_student_layout;
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        adapter = new StudentFragmentAdapter();
        myRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
        myRecycler.setAdapter(adapter);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageindex = 1;
                requestStudentList();

            }
        });
        refreshLayout.setEnableLoadmore(false);
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageindex++;
                requestStudentList();
            }
        });
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.btn_collect:
                        StudentBean.DataBean.ListBean listBean= (StudentBean.DataBean.ListBean) adapter.getItem(position);
                        qdStudent(listBean.getXsid()+"");
                        break;
                }
            }
        });
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                StudentBean.DataBean.ListBean listBean= (StudentBean.DataBean.ListBean) adapter.getItem(position);
                Intent intent=new Intent(view.getContext(), CustomerActivity.class);
                intent.putExtra("kid",listBean.getXsid());
                view.getContext().startActivity(intent);
            }
        });
        requestStudentList();
    }
//  请求学生数据
    private void requestStudentList() {
        Map<String, Object> map = new HashMap<>();
        map.put("lid", com.jsmy.marketing.manager.UserManager.getInstance().getId());
        map.put("pageindex", "" + pageindex);
        mPresenter.loadData(RetrofitManager.getInstance().studentList(map), Link.APP_STUDNET);
    }


    private void qdStudent(String xsid){
        Map<String, Object> map = new HashMap<>();
        map.put("lid", com.jsmy.marketing.manager.UserManager.getInstance().getId());
        map.put("xsid", "" + xsid);
        mPresenter.loadData(RetrofitManager.getInstance().studentqd(map), Link.APP_QD);
    }

    @Override
    public void update(){
        refreshLayout.autoRefresh();

    }
    @Override
    public void refreshData(Object response, String position) {
        switch (position){

            case Link.APP_STUDNET:
                list = ((StudentBean) response).getData().getList();
                if (pageindex == 1) {
                    refreshLayout.finishRefresh();
                    adapter.replaceData(list);
                } else {
                    refreshLayout.finishLoadmore();
                    adapter.addData(list);
                }
                break;
            case Link.APP_QD:
                BaseBean baseBean= (BaseBean) response;
                if (baseBean.getCode().equals("Y")){
                    ToastUtil.showShort(getActivity(),"抢单成功");
                    pageindex = 1;
                    requestStudentList();
                }else {
                    ToastUtil.showShort(getActivity(),baseBean.getMsg());
                }
        }
    }
}
